<!-- 车型管理 -->
<template>
  <div class="dashboard-container car-models customer-list-box">
    <div class="app-container">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="form"
          :model="searchFormData"
          label-width="80px"
        >
          <div
            class="filter-container"
          >
            <el-row
              :gutter="60"
            >
              <el-col :span="8">
                <el-form-item
                  label="车型编号:"
                  style="margin-bottom: 20px"
                >
                  <el-input
                    v-model="searchFormData.id"
                    placeholder="请输入车型编号"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="应载重量:"
                  style="margin-bottom: 20px"
                >
                  <el-select
                    v-model="searchFormData.allowableLoad"
                    clearable
                    placeholder="请选择应载重量"
                  >
                    <el-option
                      v-for="item in allowableLoadOptions"
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="应载体积:"
                  style="margin-bottom: 20px"
                >
                  <el-select
                    v-model="searchFormData.allowableVolume"
                    clearable
                    placeholder="请选择应载体积"
                  >
                    <el-option
                      v-for="item in allowableVolumOptionse"
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                    />
                  </el-select></el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="车辆类型:"
                  style="margin-bottom: 0px"
                >
                  <el-input
                    v-model="searchFormData.name"
                    placeholder="请输入车辆类型"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button
                  type="warning"
                  @click="search"
                >搜索</el-button>
                <el-button
                  class="reset-btn"
                  plain
                  @click="reset"
                >重置</el-button>
              </el-col></el-row>
          </div>
        </el-form>
      </el-card>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            type="warning"
            class="customer-add-btn"
            @click="handleCarModel()"
          >新增车型</el-button>
          <el-table
            :data="carModelsList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!carModelsList || carModelsList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-card
              v-show="
                (!carModelsList || carModelsList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column
              align="left"
              label="序号"
              type="index"
              width="60"
            >
              <template v-slot="{ $index }">
                <span>{{ $index + (searchFormData.page - 1) * searchFormData.pageSize + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="车型编号"
              min-width="180"
              prop="id"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="车辆类型"
              min-width="130"
              prop="name"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="车型数量"
              min-width="100"
              prop="num"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="应载重量（吨）"
              min-width="150"
              prop="allowableLoad"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="应载体积（立方米）"
              min-width="150"
              prop="allowableVolume"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="长（米）"
              min-width="100"
              prop="measureLong"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="宽（米）"
              min-width="100"
              prop="measureWidth"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="高（米）"
              min-width="100"
              prop="measureHigh"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              min-width="110"
              class-name="small-padding fixed-width"
            >
              <template v-slot="{row}">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="handleCarModel(row.id)"
                >编辑</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  type="danger"
                  :underline="false"
                  @click="delCarModel(row.id)"
                >删除</el-link>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <div class="pages">
              <el-pagination
                :current-page="searchFormData.page"
                :total="+total"
                :page-size="searchFormData.pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChange"
                @current-change="currentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- 车型弹窗 -->
    <CarModelDialog
      ref="carModelDialog"
      class="add-form-dialog"
      :current-node-id="currentNodeId"
      @updateCarModelsList="getList"
    />
  </div>
</template>
<script>
import { del, list } from '@/api/transit'
import CarModelDialog from './components/car-models-add.vue'
export default {
  components: {
    CarModelDialog
  },
  data() {
    return {
      listLoading: false,
      searchFormData: {
        page: 1,
        pageSize: 10,
        id: '',
        allowableLoad: null,
        allowableVolume: null,
        name: ''
      },
      total: 0,
      allowableLoadOptions: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      allowableVolumOptionse: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ],
      carModelsList: [],
      searchkey: false,
      currentNodeId: null
    }
  },
  created() {
    this.getList()
    const _this = this

    // 键盘enter操作
    document.onkeydown = function(e) {
      const key = window.event.keyCode
      if (key === 13) {
        _this.search()
      }
    }
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await list(this.searchFormData)
      this.carModelsList = data.items
      this.total = data.counts
      this.listLoading = false
    },
    handleCarModel(id) {
      if (id) {
        // 编辑车型
        this.currentNodeId = id
        this.$nextTick(() => {
          this.$refs.carModelDialog.getCarModelDetail()
          this.$refs.carModelDialog.openDialog()
        })
      } else {
        this.$refs.carModelDialog.openDialog()
      }
    },
    async delCarModel(id) {
      this.$confirm('确定要删除吗?', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await del(id)
          if (res.code === 200) {
            this.$message.success('删除车型成功')
            this.getList()
          } else if (res.code === 1) {
            this.$message.error('此车型类型下有车辆，不能删除！')
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    sizeChange(val) {
      this.searchFormData.pageSize = val
      this.getList()
    },
    currentChange(val) {
      this.searchFormData.page = val
      this.getList()
    },
    search() {
      this.searchFormData.page = 1
      this.searchkey = true
      this.getList()
    },
    reset() {
      this.searchFormData.id = ''
      this.searchFormData.allowableLoad = null
      this.searchFormData.allowableVolume = null
      this.searchFormData.name = ''
      this.searchkey = false
      this.getList()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.add-btn{
  margin-bottom: 20px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}

.el-select {
  width: 100%;
}

.car-models ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}
</style>
